<template>
  <a-modal v-model:visible="visible" title="修改关键词" :on-before-ok="confirm">
    <a-row justify="center">
      <a-form ref="formRef" :model="form" size="medium" auto-label-width :style="{ width: 'auto' }">
        <a-form-item field="category_id" label="类别" :rules="rules.category">
          <a-select
            allow-search
            v-model="form.category_id"
            placeholder="请选择类别"
            allow-clear
            :loading="loadingCategory"
          >
            <a-option v-for="item in categoryData" :value="item.value">{{ item.label }}</a-option>
          </a-select>
        </a-form-item>
        <a-form-item field="title" label="标题" :rules="rules.title">
          <a-input v-model="form.title" placeholder="请输入标题" style="width: 300px" />
        </a-form-item>
        <a-form-item field="name" label="关键词" :rules="rules.name">
          <a-textarea v-model="form.name" placeholder="请输入关键词" allow-clear />
        </a-form-item>
        <a-form-item field="status" label="状态">
          <a-switch v-model="form.status" :checked-value="0" :unchecked-value="1" />
        </a-form-item>
      </a-form>
    </a-row>
  </a-modal>
</template>

<script setup lang="ts">
import { computed, reactive, ref } from 'vue'
import { Message, Modal, type FormInstance } from '@arco-design/web-vue'
import { IconEdit, IconPlus } from '@arco-design/web-vue/es/icon'
import { getKeywordList, getKeyWordTypeList, editKeyword } from '@/apis'

type Form = { name: string; phone: string; status: boolean }
const form: Form = reactive({
  id: '',
  category_id: '',
  name: '',
  title: '',
  status: 0
})

const emits = defineEmits(['reFreshTable'])
const rules = {
  category: [{ required: true, message: '请选择类别' }],
  title: [{ required: true, message: '请输入标题' }],
  name: [{ required: true, message: '请输入关键词' }]
}

const visible = ref(false)
const detailId = ref('')
const formRef = ref<FormInstance>()

const confirm = async () => {
  const flag = await formRef.value?.validate()
  if (flag) {
    return false
  } else {
    const resData = await editKeyword({
      ...form
    })
    if (resData.code === 1) {
      Message.success('修改成功')
      visible.value = false
      form.id = ''
      form.category_id = ''
      form.name = ''
      form.title = ''
      form.status = 0
      emits('reFreshTable')
    } else {
      Message.error(resData.msg)
      return false
    }

    // return true
  }
}

const show = (id: string) => {
  form.id = id
  initKeyWords(id)
  visible.value = true
}
const initKeyWords = async (id) => {
  const res = await getKeywordList({ keyword_id: id })
  let resData = res.data?.data?.[0] || {}
  if (res.code === 1) {
    form.category_id = resData.category_id
    form.name = resData.name
    form.title = resData.title
    form.status = resData.status
  } else {
    Message.error(resData.message)
  }
}
// 获取下拉列表
const categoryPage = {
  page: 1,
  pagesize: 1000
}
const categoryData = ref([])
const loadingCategory = ref(false)
const handleLoadingCategory = (visible: boolean) => {
  if (visible) {
    loadingCategory.value = true
    getKeyWordTypeList(categoryPage).then((res) => {
      if (res.code == 1) {
        categoryData.value = (res.data.data || []).map((dd) => {
          return {
            label: dd.name,
            value: dd.id
          }
        })
      } else {
      }
      loadingCategory.value = false
    })
  }
}
handleLoadingCategory(true)
defineExpose({ show })
</script>

<style lang="scss" scoped>
:deep(.arco-collapse-item-content) {
  background: none;
}
</style>
